package com.eclab.security.app;

import com.eclab.security.app.authentication.openid.OpenIdAuthenticationSecurityConfig;
import com.eclab.security.core.authentication.mobile.SmsCodeAuthenticationSecurityConfig;
import com.eclab.security.core.authorize.AuthorizeConfigManager;
import com.eclab.security.core.properties.SecurityConstants;
import com.eclab.security.core.properties.SecurityProperties;
import com.eclab.security.core.validate.code.ValidateCodeSecurityConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
import org.springframework.security.web.session.InvalidSessionStrategy;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;
import org.springframework.social.security.SpringSocialConfigurer;

import javax.sql.DataSource;

/**
 * 描述:
 * 资源服务器配置
 * @author yd
 * @create 2019-02-17 15:41
 */
@Configuration
@EnableResourceServer
public class EclabResourceServerConfig extends ResourceServerConfigurerAdapter {

        @Autowired
        private SecurityProperties securityProperties;

        @Autowired
        private DataSource dataSource;


        @Autowired
        private SmsCodeAuthenticationSecurityConfig smsCodeAuthenticationSecurityConfig;


        @Autowired
        private SpringSocialConfigurer eclabSocialSecurityConfig;

        @Autowired
        private ValidateCodeSecurityConfig validateCodeSecurityConfig;


        @Autowired
        protected AuthenticationSuccessHandler eclabAuthenticationSuccessHandler;

        @Autowired
        protected AuthenticationFailureHandler eclabAuthenticationFailureHandler;

        @Autowired
        private OpenIdAuthenticationSecurityConfig openIdAuthenticationSecurityConfig;

        @Autowired
        private AuthorizeConfigManager authorizeConfigManager;

        @Bean
        public PersistentTokenRepository persistentTokenRepository(){

            JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();
            tokenRepository.setDataSource(dataSource);
            // tokenRepository.setCreateTableOnStartup(true);
            return tokenRepository;

        }

        @Override
        public void configure(HttpSecurity http) throws Exception {

            http.formLogin()
                    .loginPage(SecurityConstants.DEFAULT_UNAUTHENTICATION_URL)
                    .loginProcessingUrl(SecurityConstants.DEFAULT_LOGIN_PROCESSING_URL_FORM)
                    .successHandler(eclabAuthenticationSuccessHandler)
                    .failureHandler(eclabAuthenticationFailureHandler);

            http.apply(validateCodeSecurityConfig)
                    .and()
                    .apply(smsCodeAuthenticationSecurityConfig)
                        .and()
                    .apply(eclabSocialSecurityConfig)
                        .and()
                    .apply(openIdAuthenticationSecurityConfig)
                        .and()
                    .csrf().disable();

                authorizeConfigManager.config(http.authorizeRequests());
        }

}